package com.jrh.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jrh.biz.img.ImgUploadService;
import com.jrh.common.json.JacksonHelper;

/**
 * 图片上传
 * 
 * @author yout 2012-8-9 下午8:22:09
 */
@Controller
public class ImgUploadController {

    private static final String UPLOAD_TYPE = "editor";

    @Autowired
    private ImgUploadService    imgUploadService;

    @ResponseBody
    @RequestMapping(value = "upload_img", method = RequestMethod.POST)
    public String uploadImg(MultipartFile imgFile) {

        // TODO: 判断文件类型和大小
        String imgPath = imgUploadService.upload(imgFile, UPLOAD_TYPE);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("error", 0);
        map.put("url", "/htdocs" + imgPath);

        return JacksonHelper.toJSON(map);
    }

}
